(function(){
    String.prototype.allIndexOf = function(string, ignoreCase) {
        if (this === null) {
            return [-1];
        }
        var t = (ignoreCase) ? this.toString().toLowerCase() : Replace_VietNamese(this),
        s = (ignoreCase) ? string.toString().toLowerCase() : Replace_VietNamese(string.toString()),
        i = Replace_VietNamese(this).indexOf(Replace_VietNamese(s)),
        len = this.length,
        n,
        indx = 0,
        result = [];
        if (len === 0 || i === -1) {
            return [i];
        }
        for (n = 0; n <= len; n++) {
            i = t.indexOf(s, indx);
            if (i !== -1) {
                indx = i + 1;
                result.push(i);
            } else {
                return result;
            }
        }
        return result;
    }
    // To mau
    String.prototype.searchStringColor = function(string, ignoreCase) {
        if(string == '' || string == ' '){
            return this;
        }
        var result = this.allIndexOf(string,ignoreCase);
        var len = result.length;
        strlen = string.length, stroutput = '', t = 0;
        if(result[0] == -1){
            stroutput = this;
        }else{
            for (var i = 0; i < len; i++) {

                stroutput += this.substring(t,result[i]) + '<span class = "lblcolor">' + this.substring(result[i],result[i]+strlen) + '</span>';
                t = result[i]+strlen;
            }
            stroutput += this.substring(t,this.length);
        }
        return stroutput;
    }
})();
/*
* Creater: Truongdv
* Date:14/12/2012
* Idea: Class xu ly du lieu xml
*/
function efyXml(dirxml,objName,callback){    
    this.callback = callback;
    this.dirxml = dirxml;               //
    this.xml ='';
    this.loadfile();
    this.htmlexport = '';
    this.row_index = '';
    this.v_current_style_name = '';
    this.v_id_column = '';
    this.v_onclick_up = '';
    this.v_onclick_down = '';
    this.v_have_move = '';
    this.v_table = '';
    this.v_pk_column = '';
    this.v_filename_column = '';
    this.content_column = '';
    this.v_append_column = '';
    this.p_arr_item = '';
    this.display_option = '';
    this.url_exec = '';
    this.pClassname = '';
    this.objectId = '';
    this.v_width = '';
    this.v_label = '';
    this.v_align = '';
    this.columnName = '';
    this.xmlData = '';
    this.xmlTagInDb = '';
    this.v_value = '';
    this.attr = '';
    this.function_name = '';
    this.data_format ='';    
    this.objName = objName;
    this.disabledAttr = '';
    this.dbClickFn = '';
    this.title_name = '';

}
efyXml.prototype.loadfile = function(){
    var efy_xml = this;
    $.ajax({
        type: "GET",
        url: this.dirxml,
        dataType: "xml",
        ifModified:true,
        cache:false,
        success: function(xml) {            
            efy_xml.xml = xml;
            if(efy_xml.globalArraName)
                eval(efy_xml.globalArraName + '= xml');
            if (efy_xml.callback != 'undefine') {
                eval(efy_xml.callback);
            };
        }
    })
}
/*
* @author: Truongdv
* editor: Phuongtt (28/12/2012)
* @since:17/12/2012
* @todo: Xuat man hinh danh sach
* Param:    - arrResult: Mang du lieu can xuat ra
            - objupdate: Doi tuong can update toi
            - v_pk_column: Key cua col
*/
efyXml.prototype.exportTable = function(arrResult,v_pk_column,objupdate,fulltextsearch,frm,emtryRow){    
    var efy_xml = this;
    efy_xml.v_pk_column = v_pk_column;
    efy_xml.htmlexport = '';
    var xml = this.xml, iTotalRecord = arrResult.length;
    if(!v_pk_column){        
        //Lay ten cot khai bao trong file xml
        efy_xml.v_pk_column = $(xml).find('coId').text();
    }
     efy_xml.dbClickFn = $(xml).find('dbClickFn').text();          
    //Them cac thuoc tinh (neu co) vao checkbox
    if($(xml).find('attr').length > 0){
        var arrAttr = [];
        $(xml).find('attr').each(function(){
            arrAttr.push([$(this).find('attrName').text(), $(this).find('attrColID').text()]);            
        });
    }
    efy_xml.htmlexport += '<table class="list-table-data" cellpadding="0" cellspacing="0" border="0" align="center" id="table-data">';
    var psHtmlTempWidth = '', psHtmlTempLabel = '', v_type = '';
    $(xml).find('col').each(function(){
        efy_xml.v_width = $(this).find('width').text();
        efy_xml.v_label = $(this).find('label').text();
        psHtmlTempWidth += '<col width="'+ efy_xml.v_width +'">';
        psHtmlTempLabel += '<td class="title" align="center">'+ efy_xml.v_label+'</td>';
    })
    efy_xml.htmlexport += psHtmlTempWidth + '<tr class="header">'+ psHtmlTempLabel + '<tr>';
    for (var i = 0; i < iTotalRecord; i++) {
        efy_xml.objectId = arrResult[i][efy_xml.v_pk_column];
        if(efy_xml.dbClickFn != '')
            efy_xml.htmlexport += '<tr ondblclick = "'+efy_xml.objName + '.' + efy_xml.dbClickFn +',\''+efy_xml.objectId+'\')">';
        else
            efy_xml.htmlexport += '<tr>';    
            
        //render rows
        $(xml).find('col').each(function(){
            // this.attr = '';
            if(arrAttr){              
                for(var iA = 0; iA < arrAttr.length; iA++)                       
                    this.attr += arrAttr[iA][0] + '"'+ arrResult[i][arrAttr[iA][0]]; +'" ';
            }
            efy_xml.v_width = $(this).find('width').text();
            efy_xml.v_label = $(this).find('label').text();
            v_type = $(this).find('type').text();
            efy_xml.v_align = $(this).find('align').text();
            efy_xml.xmlData = $(this).find('xml_data').text();
            efy_xml.columnName = $(this).find('column_name').text();
            efy_xml.xmlTagInDb = $(this).find('xml_tag_in_db').text();
            efy_xml.v_value = arrResult[i][efy_xml.columnName];
            efy_xml.data_format = $(this).find('format').text();
            efy_xml.disabledAttr = $(this).find('disabledAttr').text();
            efy_xml.v_disabled = arrResult[i][efy_xml.disabledAttr];
            efy_xml.function_name = $(this).find('function_name').text();
            efy_xml.title_name = $(this).find('title_name').text();
            efy_xml.title_name = arrResult[i][efy_xml.title_name];

            if(efy_xml.v_disabled == 'false')
                v_attr = 'disabled='+efy_xml.v_disabled+'';
            else v_attr = '';
            if(efy_xml.data_format ==='identity'){
                 efy_xml.v_value = i+1;
            }else{
                efy_xml.v_value = arrResult[i][efy_xml.columnName];
                if(efy_xml.title_name)
                    efy_xml.v_value = '<strong title="'+efy_xml.title_name+'">'+ efy_xml.v_value +'</strong>';
            }
            if(v_type !='checkbox' &&  fulltextsearch !='undefine'){ 
                var s = new String(efy_xml.v_value);
                efy_xml.v_value = s.searchStringColor(fulltextsearch);
            }            
            efy_xml.htmlexport += efy_xml._generateHtmlForColumn(v_type,v_attr);
        })
        efy_xml.htmlexport += '</tr>';
    }    
    objupdate.html(efy_xml.htmlexport);        
    efy_xml.addRowEmpty($('form#'+frm+' table#table-data tbody'),emtryRow);
    fixTitle();
}
/**
* @author:Truongdv
* @since: 17/12/2012
* @todo: Ham xuat ra chuoi column html
* @param obj: Doi tuong duoc chon
*/
efyXml.prototype._generateHtmlForColumn = function(v_type,v_disabled){
    var htmloutput = '';
    switch(v_type) {
        case "checkbox":
            htmloutput += '<td align="'+ this.v_align + '"><input type="checkbox" onclick="selectrow(this)" name="chk_item_id" '+v_disabled+' value="'+ this.v_value +'" '+ this.attr +' />';
            htmloutput  += '</td>';
            break;
        case "text":
            htmloutput += '<td class="data" align="'+ this.v_align +'" onclick="select_row(this)">' + '&nbsp;'+ this.v_value +'&nbsp;</td>';
            break;  
        default:
            htmloutput += this.v_value;
    }
    return htmloutput;
}
/**
* @author:Truongdv
* @since: 17/12/2012
* @todo: Ham xac dinh khi NSD chon 1 row tren man hinh danh sách
* @param obj: Doi tuong duoc chon
*/

function select_row(obj){
    $('td').parent().removeClass('selected');
    $('td').parent().find('input[name="chk_item_id"]').attr('checked',false);
    if(!$(obj).parent().find('input[name="chk_item_id"]').attr('disabled')){
        $(obj).parent().addClass('selected');
        $(obj).parent().find('input[name="chk_item_id"]').attr('checked',true);
    }
}
//Them row trong
efyXml.prototype.addRowEmpty = function(obj,num){
    var numRow = $(obj).children().length - 2, numCol = $(obj).children(':first-child').children().length, shtml = '';  
    if(numRow < num){       
        for(var i = 0; i < (num - numRow); i++){
            shtml += '<tr>';
            for(var j =0; j < numCol; j++){
                shtml += '<td>&nbsp;</td>';
            }
            shtml += '</tr>';
        }
        $(obj).append(shtml);
    }
    // alert(123);
    $(obj).children('tr:odd').addClass('odd_row');
    $(obj).children('tr:even').addClass('round_row');
    $(obj).children('tr:eq(0)').removeClass('round_row');
    $(obj).children('tr:eq(1)').removeClass('odd_row');
}
/**
 * @author:TuanPM
 * @see: 28/12/2012
 * @todo: convert chu tieng viet co dau sang khong dau
 * Enter description here ...
 */
function Replace_VietNamese(str) {
     str = str.toLowerCase();
     str = str.replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g, "a");
     str = str.replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g, "e");
     str = str.replace(/ì|í|ị|ỉ|ĩ/g, "i");
     str = str.replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g, "o");
     str = str.replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g, "u");
     str = str.replace(/ỳ|ý|ỵ|ỷ|ỹ/g, "y");
     str = str.replace(/đ/g, "d");
     return str;
     }